package cn.ii8080.i8.module.account.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;

public class BaseController<T> {

    @Autowired
    protected IService<T> iService;

    //增加
    @PostMapping
    public Boolean save(@RequestBody T t) {
        return iService.save(t);
    }

    //删除
    @DeleteMapping("/{id}")
    public Boolean remove(@PathVariable("id") Serializable id) {
        return iService.removeById(id);
    }

    //修改
    @PutMapping
    public Boolean update(@RequestBody T t) {
        return iService.updateById(t);
    }

    @GetMapping("/one/{column}/{val}")
    public T getOne(@PathVariable("column") String column, @PathVariable("val") String val) {
        QueryWrapper<T> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(column, val);
        return iService.getOne(queryWrapper);
    }

    @GetMapping("/list/{column}/{val}")
    public List<T> getList(@PathVariable("column") String column, @PathVariable("val") String val) {
        QueryWrapper<T> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(column, val);
        return iService.list(queryWrapper);
    }

    @GetMapping("/{id}")
    public T getById(@PathVariable("id") Serializable id) {
        return iService.getById(id);
    }
}
